<template>
  <el-form ref="form" :model="user" :rules="rules" label-width="80px">
    <el-form-item :label="$t('old_rassword')" prop="oldPassword">
      <el-input
        v-model="user.oldPassword"
        :placeholder="$t('please_enter', { name: $t('old_rassword') })"
        type="password"
        show-password
      />
    </el-form-item>
    <el-form-item :label="$t('new_rassword')" prop="newPassword">
      <el-input
        v-model="user.newPassword"
        :placeholder="$t('please_enter', { name: $t('new_rassword') })"
        type="password"
        show-password
      />
    </el-form-item>
    <el-form-item :label="$t('confirm_password')" prop="confirmPassword">
      <el-input
        v-model="user.confirmPassword"
        :placeholder="$t('confirm_password')"
        type="password"
        show-password
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="mini" @click="submit">{{
        $t("save")
      }}</el-button>
      <el-button type="danger" size="mini" @click="close">{{
        $t("cancel")
      }}</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserPwd } from "@/api/system/user";

export default {
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error(this.$t("passwords_differ")));
      } else {
        callback();
      }
    };
    return {
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined,
      },
      // 表单校验
      rules: {
        oldPassword: [
          {
            required: true,
            message: this.$t("cannot_empty", { name: this.$t("old_rassword") }),
            trigger: "blur",
          },
        ],
        newPassword: [
          {
            required: true,
            message: this.$t("cannot_empty", { name: this.$t("new_rassword") }),
            trigger: "blur",
          },
          {
            min: 6,
            max: 20,
            message: this.$t("length_between", {
              name1: this.$t("password"),
              name2: "6",
            }),
            trigger: "blur",
          },
        ],
        confirmPassword: [
          {
            required: true,
            message: this.$t("cannot_empty", {
              name: this.$t("confirm_password"),
            }),
            trigger: "blur",
          },
          { required: true, validator: equalToPassword, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          updateUserPwd(this.user.oldPassword, this.user.newPassword).then(
            (response) => {
              this.$modal.msgSuccess(this.$t("operation_successful"));
            }
          );
        }
      });
    },
    close() {
      this.$tab.closePage();
    },
  },
};
</script>
